﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Common.Entities;
using Common.Enums;
using Lidgren.Network;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ShootLife2D.GUI.Controls;
using TomShane.Neoforce.Controls;

namespace ClickGame.Screens
{
    public class LoginScreen : Screen
    {
        public string LoginText
        {
            get { return _login.Text; }
        }
        public string PasswordText
        {
            get { return _password.Text; }
        }
        private Game1 _game;
        private Manager _manager;
        private TextBox _login;
        private TextBox _password;
        private Button _loginBtn;
        private Button _exitBtn;
        private Label _messageLbl;
        private Window _window;
        //private Texture2D _errorImg;
        private Texture2D _waitImg;
        //private Texture2D _questionImg;
        public Dictionary<MessageType, Texture2D> _msgImgs;//массив с иконками сообщений
        private AnimatedImageBox _imageBox;

        //Окно выбора персонажа
        private Window _characterSelectionWindow;
        private ListBox _charsLb;
        private Button _charSelect;
        private Button _newChar;
        private Button _delChar;
        private Label _infoLabel;
        //----------------------------------------------
        public LoginScreen(Game1 game)
        {
            _game = game;
            //_manager = new Manager(game, game.Graphics, "Default");
            //_manager.SkinDirectory = "Content\\Skins\\";
            //_manager.AutoCreateRenderTarget = true;
            _manager = _game.Manager;
            _manager.SkinDirectory = "Content\\Skins\\";
            _manager.AutoCreateRenderTarget = true;
            _msgImgs = new Dictionary<MessageType, Texture2D>();
            //_manager.RenderTarget = game.GraphicsDevice.GetRenderTargets()[0].RenderTarget;
            //LoadContent(_game.Content);
        }
        public override void LoadContent(Microsoft.Xna.Framework.Content.ContentManager content)
        {
            _msgImgs.Add(MessageType.Error, (Texture2D)Game1.Resources.GetResource("Skins\\Default\\Images\\Icon.Error", typeof(Texture2D)));//content.Load<Texture2D>("Skins\\Default\\Images\\Icon.Error")));
            _msgImgs.Add(MessageType.Question, (Texture2D)Game1.Resources.GetResource("Skins\\Default\\Images\\Icon.Question", typeof(Texture2D)));
            _msgImgs.Add(MessageType.General, (Texture2D)Game1.Resources.GetResource("Skins\\Default\\Images\\Icon.Information", typeof(Texture2D)));
            _msgImgs.Add(MessageType.Warning, (Texture2D)Game1.Resources.GetResource("Skins\\Default\\Images\\Icon.Warning", typeof(Texture2D)));
            //_msgImgs.Add(MessageType.Question, content.Load<Texture2D>("Skins\\Default\\Images\\Icon.Question"));
            //_msgImgs.Add(MessageType.General, content.Load<Texture2D>("Skins\\Default\\Images\\Icon.Information"));
            //_msgImgs.Add(MessageType.Warning, content.Load<Texture2D>("Skins\\Default\\Images\\Icon.Warning"));
            _waitImg = content.Load<Texture2D>("Images\\GUI\\wait");
            //_manager.Initialize();

            _window = new Window(_manager);
            _window.Init();
            _window.Text = "Вход";
            _window.Width = 400;
            _window.Height = 400;
            _window.Center();
            _window.Movable = false;
            _window.Resizable = false;
            _window.CloseButtonVisible = false;
            //_window.CloseButtonVisible = true;
            _window.Visible = true;

            _login = new TextBox(_manager);
            _login.Init();
            //_login.ColorMode = true;
            _login.Parent = _window;
            _login.Height = 20;
            _login.Width = 350;
            _login.SetPosition(25, 25);
            _login.Text = "Логин";
            //_login.CanSelect = false;
            //_login.CaretVisible = true;

            _password = new TextBox(_manager);
            _password.Init();
            _password.Parent = _window;
            _password.Height = 20;
            _password.Width = 350;
            _password.SetPosition(25, 50);
            _password.Text = "Пароль";
            _password.PasswordChar = '*';
            _password.Mode = TextBoxMode.Password;
            _password.KeyPress += new KeyEventHandler(_password_KeyPress);

            _loginBtn = new Button(_manager);
            _loginBtn.Init();
            _loginBtn.Parent = _window;
            _loginBtn.Height = 20;
            _loginBtn.Width = 170;
            _loginBtn.SetPosition(25, 75);
            _loginBtn.Text = "Войти";
            _loginBtn.Click += new TomShane.Neoforce.Controls.EventHandler(_loginBtn_Click);

            _exitBtn = new Button(_manager);
            _exitBtn.Init();
            _exitBtn.Parent = _window;
            _exitBtn.Height = 20;
            _exitBtn.Width = 170;
            _exitBtn.SetPosition(205, 75);
            _exitBtn.Text = "Выйти";
            _exitBtn.Click += new TomShane.Neoforce.Controls.EventHandler(_exitBtn_Click);

            _messageLbl = new Label(_manager);
            _messageLbl.Init();
            _messageLbl.Parent = _window;
            _messageLbl.Height = 20;
            _messageLbl.Width = 150;
            _messageLbl.SetPosition(25, 100);
            _messageLbl.Visible = false;
            _messageLbl.Color = Color.Red;

            _imageBox = new AnimatedImageBox(new Vector2(256, 256), _waitImg, _manager);
            _imageBox.Init();
            _imageBox.Parent = _window;
            _imageBox.Image = _waitImg;
            _imageBox.Frames = 32;
            _imageBox.Width = 128;
            _imageBox.Height = 128;
            _imageBox.SetPosition(134, 120);
            _imageBox.Visible = false;

            _manager.Add(_window);

            base.LoadContent(content);
        }

        public void Login()
        {
            _imageBox.Visible = true;
            new Thread(() =>
            {
                _game.ConnectToServer(_login.Text, _password.Text);
            }).Start();
        }
        public void LoginResult(string errorMessage)
        {
            if (errorMessage != null)
            {
                _imageBox.Visible = false;
                ShowMessage(errorMessage, "Ошибка", MessageType.Error);
                return;
            }
            _imageBox.Visible = false;
            //ShowMessageQuestion(accountInfo, "Вопрос");
        }

        public override void ShowMessage(string errorMessage, string title, MessageType type)
        {
            switch (type)
            {
                case MessageType.General:
                    break;
                case MessageType.Question:
                    break;
                case MessageType.Warning:
                    break;
                case MessageType.Error:
                    _imageBox.Visible = false;
                    ShowMessage(errorMessage, "Ошибка", MessageType.Error, new TomShane.Neoforce.Controls.EventHandler(btn_Click),null);
                    break;
            }
        }

        public void SuccessOnDeleting(string name)
        {
            //ShowMessage(charId.ToString(), "");
            //_charsLb.Items.Remove(_charsLb.Items.First(lbi => lbi.ToString().Split(',')[0] == name));//я бы сделал либо StartWith либо Contains)
        }

        void _password_KeyPress(object sender, KeyEventArgs e)
        {
            if (e.Key == Keys.Enter)
            {
                Login();
            }
        }

        void _exitBtn_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            _game.Exit();
        }

        void _loginBtn_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            Login();
        }
        public void ShowMessage(string message, string title, MessageType type, TomShane.Neoforce.Controls.EventHandler okBtnClick, TomShane.Neoforce.Controls.EventHandler noBtnClick)
        {
            Window msg = new Window(_manager);

            TextBox tb = new TextBox(_manager);
            ImageBox img = new ImageBox(_manager);
            msg.Init();

            tb.Init();
            img.Init();
            switch (type)
            {
                case MessageType.General:
                case MessageType.Warning:
                case MessageType.Error:
                    Button btn = new Button(_manager);
                    btn.Init();
                    btn.SetPosition(150, 130);
                    btn.Text = "OK";
                    btn.Height = 20;
                    btn.Width = 100;
                    btn.Parent = msg;
                    btn.Click += okBtnClick;//new TomShane.Neoforce.Controls.EventHandler(btn_Click);
                    break;
                case MessageType.Question:
                    Button btnYes = new Button(_manager);
                    Button btnNo = new Button(_manager);
                    btnYes.Init();
                    btnNo.Init();
                    btnYes.SetPosition(96, 130);
                    btnYes.Text = "Да";
                    btnYes.Height = 20;
                    btnYes.Width = 100;
                    btnYes.Parent = msg;
                    btnYes.Click += okBtnClick;//new TomShane.Neoforce.Controls.EventHandler(btnYes_Click);

                    btnNo.SetPosition(204, 130);
                    btnNo.Text = "Нет";
                    btnNo.Height = 20;
                    btnNo.Width = 100;
                    btnNo.Parent = msg;
                    btnNo.Click += noBtnClick;//new TomShane.Neoforce.Controls.EventHandler(btn_Click);
                    break;
            }


            msg.Width = 400;
            msg.Height = 200;
            msg.Text = title;
            msg.Resizable = false;
            msg.CloseButtonVisible = false;
            msg.Movable = false;
            msg.StayOnTop = true;
            msg.Center();
            //msg.Top = int.MaxValue;

            img.Image = _msgImgs[type];
            img.Parent = msg;
            img.SetPosition(10, 10);

            tb.SetPosition(70, 10);
            tb.Width = 300;
            tb.Height = 120;
            //tb.Mode = TextBoxMode.Multiline;
            tb.WordWrapping = true;
            tb.Text = message;
            tb.Parent = msg;
            tb.DrawBorders = false;
            tb.ReadOnly = true;
            tb.CanFocus = false;
            tb.TextColor = Color.Red;
            //tb.Init();




            _manager.Add(msg);
        }

        //public void ShowMessageQuestion(string message, string title)
        //{
        //    //if (_questionMsg != null)
        //    //{
        //    //    return;
        //    //}
        //    _questionMsg = new Window(_manager);
        //    Button btnYes = new Button(_manager);
        //    Button btnNo = new Button(_manager);
        //    TextBox tb = new TextBox(_manager);
        //    ImageBox img = new ImageBox(_manager);
        //    _questionMsg.Init();
        //    btnYes.Init();
        //    btnNo.Init();
        //    tb.Init();
        //    img.Init();

        //    _questionMsg.Width = 400;
        //    _questionMsg.Height = 200;
        //    _questionMsg.Text = title;
        //    _questionMsg.Resizable = false;
        //    _questionMsg.CloseButtonVisible = false;
        //    _questionMsg.Movable = false;
        //    _questionMsg.StayOnTop = true;
        //    _questionMsg.Center();
        //    //msg.Top = int.MaxValue;

        //    img.Image = _questionImg;
        //    img.Parent = _questionMsg;
        //    img.SetPosition(10, 10);

        //    tb.SetPosition(70, 10);
        //    tb.Width = 300;
        //    tb.Height = 120;
        //    //tb.Mode = TextBoxMode.Multiline;
        //    tb.Text = message;
        //    tb.Parent = _questionMsg;
        //    tb.DrawBorders = false;
        //    tb.ReadOnly = true;
        //    tb.CanFocus = false;
        //    tb.TextColor = Color.Red;
        //    //tb.Init();

        //    btnYes.SetPosition(96, 130);
        //    btnYes.Text = "Да";
        //    btnYes.Height = 20;
        //    btnYes.Width = 100;
        //    btnYes.Parent = _questionMsg;
        //    btnYes.Click += new TomShane.Neoforce.Controls.EventHandler(btnYes_Click);

        //    btnNo.SetPosition(204, 130);
        //    btnNo.Text = "Нет";
        //    btnNo.Height = 20;
        //    btnNo.Width = 100;
        //    btnNo.Parent = _questionMsg;
        //    btnNo.Click += new TomShane.Neoforce.Controls.EventHandler(btn_Click);

        //    _manager.Add(_questionMsg);
        //}

        void btnYes_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            //DeleteChar();
            Button btn = (Button)sender;
            Window wnd = (Window)((ClipBox)btn.Parent).Parent;
            wnd.Close();
        }

        void btn_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            Button btn = (Button)sender;
            Window wnd = (Window)((ClipBox)btn.Parent).Parent;
            wnd.Close();
            //throw new NotImplementedException();
        }
        public override void Draw(GameTime gameTime)
        {
            if (Initialized)
            {
                _manager.BeginDraw(gameTime);
                _game.GraphicsDevice.Clear(Color.Black);

                _manager.EndDraw();
                base.Draw(gameTime);
            }
        }
        public override void Update(GameTime gameTime)
        {
            if (Initialized)
            {
                _manager.Update(gameTime);
                _imageBox.UpdateImg(gameTime);
                base.Update(gameTime);
            }
        }
        public override void Dispose()
        {
            _window.Close();
            _manager.Remove(_window);
            _messageLbl.Dispose();
            _messageLbl = null;
            _password.Dispose();
            _password = null;
            _loginBtn.Dispose();
            _loginBtn = null;
            _login.Dispose();
            _login = null;
            _exitBtn.Dispose();
            _exitBtn = null;
            //_errorImg.Dispose();
            //_errorImg = null;
            _msgImgs = null;
            _window.Dispose();
            _window = null;
            //_manager.Dispose();
            _manager = null;
            _game = null;
            base.Dispose();
        }
    }
}
